package com.woniuxy.yoga.notice.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.AdminState;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.notice.model.dto.systemnotice.SystemNoticeDTO;
import com.woniuxy.yoga.notice.model.form.systemnotice.ListAllSystemNoticeForm;
import com.woniuxy.yoga.notice.model.form.systemnotice.PublishSystemNoticeForm;
import com.woniuxy.yoga.notice.model.param.systemnotice.ListAllSystemNoticeParam;
import com.woniuxy.yoga.notice.model.param.systemnotice.PublishSystemNoticeParam;
import com.woniuxy.yoga.notice.permission.Permission;
import com.woniuxy.yoga.notice.service.SystemNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/systemNotice")
@Api(tags = "系统公告相关接口")
public class SystemNoticeController {

    @Autowired
    private SystemNoticeService systemNoticeService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 管理员发布系统公告
     * @param form
     * @param authen_token
     * @return
     */
    @PostMapping("/publishSystemNotice.api")
    @ApiOperation("管理员发布系统公告")
    @YogaLog("管理员发布系统公告")
    @Permission(resources = {"/notice/systemNotice/publishSystemNotice.api"})
    public Result<String> publishSystemNotice(@RequestBody PublishSystemNoticeForm form, @RequestHeader String authen_token){
        String refreshToken = redisUtil.get(authen_token);
        Integer accountId = JWTUtil.getAdminId(refreshToken);
        PublishSystemNoticeParam param = BeanUtil.toBean(form, PublishSystemNoticeParam.class);
        param.setAccountId(accountId);
        systemNoticeService.publishSystemNotice(param);
        return Result.getResult(String.class).setMessage("发布系统公告成功");
    }

    /**
     * 管理员停用系统公告
     * @param systemNoticeId
     * @param authen_token
     * @return
     */
    @GetMapping("/disableSystemNotice/{systemNoticeId}.api")
    @ApiOperation("管理员停用系统公告")
    @YogaLog("管理员停用系统公告")
    @Permission(resources = {"/notice/systemNotice/disableSystemNotice.api"})
    @ApiImplicitParam(value = "系统公告ID",name = "systemNoticeId")
    public Result<String> disableSystemNotice(@PathVariable("systemNoticeId") Integer systemNoticeId,@RequestHeader String authen_token){
        String refreshToken = redisUtil.get(authen_token);
        Integer accountId = JWTUtil.getAdminId(refreshToken);
        systemNoticeService.disableSystemNotice(systemNoticeId,accountId);
        return Result.getResult(String.class).setMessage("停用系统公告成功");
    }

    /**
     * 管理员启用系统公告
     * @param systemNoticeId
     * @param authen_token
     * @return
     */
    @GetMapping("/normalSystemNotice/{systemNoticeId}.api")
    @ApiOperation("管理员启用系统公告")
    @YogaLog("管理员启用系统公告")
    @Permission(resources = {"/notice/systemNotice/normalSystemNotice.api"})
    @ApiImplicitParam(value = "系统公告ID",name = "systemNoticeId")
    public Result<String> normalSystemNotice(@PathVariable("systemNoticeId") Integer systemNoticeId,@RequestHeader String authen_token){
        String refreshToken = redisUtil.get(authen_token);
        Integer accountId = JWTUtil.getAdminId(refreshToken);
        systemNoticeService.normalSystemNotice(systemNoticeId,accountId);
        return Result.getResult(String.class).setMessage("启用系统公告成功");
    }

    /**
     * 管理员删除系统公告
     * @param systemNoticeId
     * @param authen_token
     * @return
     */
    @GetMapping("/delSystemNotice/{systemNoticeId}.api")
    @ApiOperation("管理员删除系统公告")
    @YogaLog("管理员删除系统公告")
    @Permission(resources = {"/notice/systemNotice/delSystemNotice.api"})
    @ApiImplicitParam(value = "系统公告ID",name = "systemNoticeId")
    public Result<String> delSystemNotice(@PathVariable("systemNoticeId") Integer systemNoticeId,@RequestHeader String authen_token){
        String refreshToken = redisUtil.get(authen_token);
        Integer accountId = JWTUtil.getAdminId(refreshToken);
        systemNoticeService.delSystemNotice(systemNoticeId,accountId);
        return Result.getResult(String.class).setMessage("删除系统公告成功");
    }

    /**
     * 管理员分页查询全部系统公告
     * @param form
     * @return
     */
    @PostMapping("/listAllSystemNotice.api")
    @ApiOperation("管理员查询全部系统公告")
    @YogaLog("管理员查询全部系统公告")
    @Permission(resources = {"/notice/systemNotice/listAllSystemNotice.api"})
    public Result<PageInfo> listAllSystemNotice(@RequestBody ListAllSystemNoticeForm form){
        ListAllSystemNoticeParam param = BeanUtil.toBean(form, ListAllSystemNoticeParam.class);
        PageInfo pageInfo = systemNoticeService.listAllSystemNotice(param);
        return Result.getResult(PageInfo.class).setMessage("查询全部公告成功").setData(pageInfo);
    }

    /**
     * 管理员分页查询已发布系统公告
     * @param form
     * @return
     */
    @PostMapping("/listPuSystemNotice.api")
    @ApiOperation("管理员查询已发布系统公告")
    @YogaLog("管理员查询已发布系统公告")
    @Permission(resources = {"/notice/systemNotice/listPuSystemNotice.api"})
    public Result<PageInfo> listPuSystemNotice(@RequestBody ListAllSystemNoticeForm form){
        ListAllSystemNoticeParam param = BeanUtil.toBean(form, ListAllSystemNoticeParam.class);
        param.setNoticeState(AdminState.SYSTEM_NOTICE_PUBLISHED);
        PageInfo pageInfo = systemNoticeService.listAllSystemNotice(param);
        return Result.getResult(PageInfo.class).setMessage("查询正常公告成功").setData(pageInfo);
    }

    /**
     * 查看最新公告
     * @return
     */
    @GetMapping({"/loadLastSystemNotice.bu", "/loadLastSystemNotice.api"})
    @ApiOperation("查看最新系统公告")
    @YogaLog("查看最新系统公告")
    public Result<SystemNoticeDTO> loadLastSystemNotice(){
        SystemNoticeDTO systemNoticeDTO = systemNoticeService.loadLastSystemNotice();
        return Result.getResult(SystemNoticeDTO.class).setMessage("查询最新公告成功").setData(systemNoticeDTO);
    }

}

